<!--添加字段-->
<template>
  <div>
    <el-dialog title="添加字段" :visible.sync="dialogVisible" :close-on-click-modal="false" width="90%">
        <div class="my-form-container" style="height:576px">
            <el-row class="my-form-item" style="height:120px">
                <el-col :span="24">
                    <div class="my-form-item-label">被授予角色</div>
                    <div class="my-form-item-value">
                      <el-checkbox-group v-model="checkRoleList">
                        <el-checkbox v-for="(i,index) in roleList" :key="index" :label="i.id">{{i.roleName}}</el-checkbox>
                      </el-checkbox-group>
                    </div>
                </el-col>
            </el-row>
            <el-row class="my-form-item" style="height:134px">
                <el-col :span="24">
                    <div class="my-form-item-label">选择分类</div>
                    <div class="my-form-item-value">
                       <el-checkbox-group v-model="checkList" @change="choseType">
                        <el-checkbox v-for="(i,index) in typeList" :key="index" :label="i.value">{{i.label}}</el-checkbox>
                      </el-checkbox-group>
                    </div>
                </el-col>
            </el-row>
            <el-row class="my-form-item" style="border:none;height:320px">
                <el-col :span="24">
                    <div class="my-form-item-label">选择填报字段</div>
                    <div class="my-form-item-value">
                      <div>
                        <el-input class="width344" v-for="i in dataList" :key="i.flcid" v-model="i.fieldName" :disabled="i.isFixed ==1"></el-input>
                      </div>
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="button-group" slot="footer">
          <div class="" @click="save">保存</div>
          <div class="" @click="dialogVisible=false">取消</div>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import {dataExtendList,dataExtendListSave,roleList} from "@/api/lineData";

export default {
  data() {
    return {
      dialogVisible:false,
      checkList:[],
      checkRoleList:[],
      typeList:[
        {label:'个人信息',value:'个人信息'},
        {label:'时间信息',value:'时间信息'},
        {label:'授课信息',value:'授课信息'},
        {label:'状态信息',value:'状态信息'},
        {label:'听课信息',value:'听课信息'},
        {label:'跟课信息',value:'跟课信息'},
        {label:'评价信息',value:'评价信息'},
      ],
      roleList:[],
      dataList:[]
    };
  },
  mounted(){
    this.initList();
    this.getRole();
  },
  methods: {
    async initList(){
      let res = await dataExtendList()
      this.dataList = res.data
    },
    async getRole(){
      let res = await roleList();
      this.roleList = res.data;
    },
    choseType(e){
      this.checkList = this.checkList.slice(-1)
    },
    async save(){
      let data = [];
      for(let i=0;i<this.dataList.length;i++){
        if(this.dataList[i].fieldName !=='未命名'){
          console.log(this.checkList[0])
          data.push({
            ...this.dataList[i],
            fieldName:this.dataList[i].fieldName,
            flcid:this.dataList[i].flcid,
            fieldGroup:this.checkList[0]
          })
        }else{
          data.push({
            ...this.dataList[i]
          })
        }
      }
      console.log(data)
      let params = {
        fieldInfos:data,
        roles:this.checkRoleList
      }
      let res = await dataExtendListSave(params);
      if(res.code == 200){
        this.$message.success('保存成功')
        this.$emit('addCodeValue')
        this.dialogVisible = false;
      }
    }
  },
};
</script>
<style lang="less" scoped>
@import "../../../../styles/commonCss.less";
/deep/.button-group{
  padding-bottom: 48px;
  margin-top: 0;
}
.my-form-container{width: 80%;}
.width90{width: 90px;}
.width344{width: 344px;margin-right: 26px;}
/deep/ .my-form-item-label{
  justify-content: center;
  padding-left: 0!important;
}
</style>
